package com.tzstcl.sys.user.ctrl;


import com.tzstcl.base.ctrl.BaseCtrl;
import com.tzstcl.framework.config.SysConfig;
import com.tzstcl.framework.shiro.ShiroUtils;
import com.tzstcl.sys.user.WTTMenuUtil;
import com.tzstcl.sys.user.model.SysMenu;
import com.tzstcl.sys.user.service.impl.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 登录控制器
 *
 * @author haocj
 */
@Controller
@RequestMapping("/admin")
public class AdminCtrl extends BaseCtrl {

    @Autowired
    private SysMenuService menuService;
    /**
     * 系统基础配置
     */
    @Autowired
    private SysConfig sysConfig;

    /**
     * 登陆页面
     *
     * @param request
     * @return
     */
    @RequestMapping("/loginPage")
    public String index(HttpServletRequest request, Model model) {
        model.addAttribute("systemName", sysConfig.getName());
        return "admin/login";
    }


    /**
     * 首页
     *
     * @return
     */
    @RequestMapping("/index")
    public String mainIndex(SysMenu sysMenu, Model model) {
//        List<SysMenu> listAll = menuService.findListAll(sysMenu);
//        model.addAttribute("menus",rootTree(listAll));
        List<SysMenu> listAll = null;
        if (1 == ShiroUtils.getUserId()) {
            listAll = menuService.findListAllMenu();
        } else {
            listAll = menuService.findUserMenu(ShiroUtils.getUserId());
        }
        model.addAttribute("systemName", sysConfig.getName());
        model.addAttribute("menus", WTTMenuUtil.getWttMenusString(listAll));
        return "admin/index";
    }

    /**
     * 权限丢失页面
     *
     * @return
     */
    @RequestMapping("/hasPermissionError")
    public String permission() {
        return "admin/sys/permission";
    }

    /**
     * 菜单列表
     *
     * @return
     */
    @RequestMapping(value = "/sysMenu/list")
    public String getMenuInfo() {
        return "admin/sys/sys_menu";
    }


    /**
     * 岗位列表
     *
     * @return
     */
    @RequestMapping(value = "/sysPost/list")
    public String psotList() {
        return "admin/sys/sys_post";
    }


    /**
     * 欢迎页面
     *
     * @return
     */
    @RequestMapping(value = "/sys/welcome")
    public String welcome() {
        return "admin/welcome";
    }


    public static List<SysMenu> rootTree(List<SysMenu> list) {

        List<SysMenu> root = new ArrayList<>();
        for (SysMenu sysMenu : list) {
            if (null == sysMenu.getParentId() || "".equals(sysMenu.getParentId())) {
                root.add(sysMenu);
            }
        }

        for (SysMenu sysMenu : root) {
            List<SysMenu> children = children(sysMenu.getId() + "", list);
            sysMenu.setSysMenus(children);
        }

        return root;
    }

    private static List<SysMenu> children(String id, List<SysMenu> list) {
        List<SysMenu> childList = new ArrayList<>();
        for (SysMenu sysMenu : list) {
            if (sysMenu.getParentId().equals(id)) {
                childList.add(sysMenu);
            }
        }
        for (SysMenu sysMenu : childList) {
            sysMenu.setSysMenus(children(sysMenu.getId() + "", list));
        }
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }

}
